var todayData,allData;
var nurseid = ''
var errorObj=(function ($) {
	var todayPull,allPull;
	var todayStart = 0, allStart = 0,Limit = 20;
	var todayPulling = false,allPulling = false;
	
	
	$.init();
	$.plusReady(function () {
	    baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	
	//今日错题
	function getTodayData(){
		if (todayPulling) {
			todayPull.endPullUpToRefresh()
			return;
		}
		todayPulling = true
		var pdict = {'userId':nurseid,'par':nurseid}
		var paramDict = {'method':'web.INMTeaComm:FindLearnTheoryTest','params':JSON.stringify(pdict),'type':'Method'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var dataArr = result.data
			var dataArr = [{"rw":"1","TheoryDesc":"三基理论","children":[{"rw":"3","TheoryDesc":"护理程序","children":[{"rw":"12","TheoryDesc":"护理程序题目1","SingleNum":"1","MultipleNum":"0","BlankNum":"0","AllNum":"1","AnswerNum":"1","ErrorNum":"0"}],"SingleNum":"1","MultipleNum":"0","BlankNum":"0","AllNum":"1","AnswerNum":"1","ErrorNum":""},{"rw":"4","TheoryDesc":"护理执业防护","children":[{"rw":"11","TheoryDesc":"CS","SingleNum":"0","MultipleNum":"0","BlankNum":"0","AllNum":"0","AnswerNum":"0","ErrorNum":"0"}],"SingleNum":"0","MultipleNum":"0","BlankNum":"0","AllNum":"0","AnswerNum":"0","ErrorNum":""},{"rw":"5","TheoryDesc":"医院和住院环境","SingleNum":"11","MultipleNum":"0","BlankNum":"0","AllNum":"11","AnswerNum":"11","ErrorNum":"0"}],"SingleNum":"12","MultipleNum":"0","BlankNum":"0","AllNum":"12","AnswerNum":"12","ErrorNum":""},{"rw":"2","TheoryDesc":"专科理论","children":[{"rw":"6","TheoryDesc":"消化系统解剖生理","SingleNum":"6","MultipleNum":"0","BlankNum":"0","AllNum":"6","AnswerNum":"2","ErrorNum":"0"}],"SingleNum":"6","MultipleNum":"0","BlankNum":"0","AllNum":"6","AnswerNum":"2","ErrorNum":""},{"rw":"7","TheoryDesc":"应急预案","SingleNum":"5","MultipleNum":"0","BlankNum":"0","AllNum":"5","AnswerNum":"0","ErrorNum":"0"},{"rw":"13","TheoryDesc":"臧阳爽理论题目","SingleNum":"5","MultipleNum":"1","BlankNum":"1","AllNum":"7","AnswerNum":"0","ErrorNum":"0"}]
			if (!dataArr) {
				todayPull.endPullUpToRefresh();
				todayPulling = false
				return;
			}
			if (todayStart == 0) {
				todayData = dataArr;
				$('#todayUL')[0].innerHTML = ''
			}else{
				todayData = todayData.concat(dataArr)
			}
			//console.log(todayData)
			updateExamUI(dataArr,'todayUL',0)
			if (dataArr.length < Limit) {
				todayPull.endPullUpToRefresh(true);
			}else{
				todayPull.endPullUpToRefresh();
			}
			todayStart += dataArr.length;
			todayPulling = false
		}, function(errorStr) {
		 	todayPull.endPullUpToRefresh();
		 	todayPulling = false
		});
	}

	//全部错题
	function getAllData(){
		if (allPulling) {
			allPull.endPullUpToRefresh()
			return;
		}
		allPulling = true
		
		var pdict = {'userId':nurseid,'par':nurseid}
		var paramDict = {'method':'web.INMTeaComm:FindLearnTheoryTest','params':JSON.stringify(pdict),'type':'Method'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var dataArr = result.data
			var dataArr = [{"rw":"1","TheoryDesc":"三基理论","children":[{"rw":"3","TheoryDesc":"护理程序","children":[{"rw":"12","TheoryDesc":"护理程序题目1","SingleNum":"1","MultipleNum":"0","BlankNum":"0","AllNum":"1","AnswerNum":"1","ErrorNum":"0"}],"SingleNum":"1","MultipleNum":"0","BlankNum":"0","AllNum":"1","AnswerNum":"1","ErrorNum":""},{"rw":"4","TheoryDesc":"护理执业防护","children":[{"rw":"11","TheoryDesc":"CS","SingleNum":"0","MultipleNum":"0","BlankNum":"0","AllNum":"0","AnswerNum":"0","ErrorNum":"0"}],"SingleNum":"0","MultipleNum":"0","BlankNum":"0","AllNum":"0","AnswerNum":"0","ErrorNum":""},{"rw":"5","TheoryDesc":"医院和住院环境","SingleNum":"11","MultipleNum":"0","BlankNum":"0","AllNum":"11","AnswerNum":"11","ErrorNum":"0"}],"SingleNum":"12","MultipleNum":"0","BlankNum":"0","AllNum":"12","AnswerNum":"12","ErrorNum":""},{"rw":"2","TheoryDesc":"专科理论","children":[{"rw":"6","TheoryDesc":"消化系统解剖生理","SingleNum":"6","MultipleNum":"0","BlankNum":"0","AllNum":"6","AnswerNum":"2","ErrorNum":"0"}],"SingleNum":"6","MultipleNum":"0","BlankNum":"0","AllNum":"6","AnswerNum":"2","ErrorNum":""},{"rw":"7","TheoryDesc":"应急预案","SingleNum":"5","MultipleNum":"0","BlankNum":"0","AllNum":"5","AnswerNum":"0","ErrorNum":"0"},{"rw":"13","TheoryDesc":"臧阳爽理论题目","SingleNum":"5","MultipleNum":"1","BlankNum":"1","AllNum":"7","AnswerNum":"0","ErrorNum":"0"}]
			if (!dataArr) {
				allPull.endPullUpToRefresh();
				allPulling = false
				return;
			}
			if (allStart == 0) {
				$('#allUL')[0].innerHTML = ''
				allData = dataArr;
			}else{
				allData = allData.concat(dataArr)
			}
			//console.log(todayData)
			updateExamUI(dataArr,'allUL',1)
			if (dataArr.length < Limit) {
				allPull.endPullUpToRefresh(true);
			}else{
				allPull.endPullUpToRefresh();
			}
			allStart += dataArr.length;
			allPulling = false
		}, function(errorStr) {
		 	allPull.endPullUpToRefresh();
		 	allPulling = false
		});
	}
	
	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		var slider = document.getElementById('slider');
		slider.addEventListener('slide', function(event) {
			var index = event.detail.slideNumber;
			refreshMainPage(index)
			$('#sliderIndicator .mui-indicator[data-index="'+index+'"]')[0].classList.add('mui-active');
		});
		var indicators = document.querySelectorAll('#sliderIndicator .mui-indicator');
		for (var i = 0; i < indicators.length;i++) {
			indicators[i].addEventListener('tap',function(){
				var oldActive = $('#sliderIndicator .mui-indicator.mui-active')[0];
				if (oldActive) {
					if (oldActive == this) {
						return
					}
					oldActive.classList.remove('mui-active')
				}
				var newIndex = parseInt(this.getAttribute('data-index'))
				refreshMainPage(newIndex)
				this.classList.add('mui-active')
				mui('#slider').slider().gotoItem(newIndex,100)
			});
		}
		var todayScroll = document.getElementById('todayScroll');
		todayPull = $(todayScroll).pullToRefresh({
			up: {
				callback: function() {
					getTodayData()
				}
			}
		});
		var allScroll = document.getElementById('allScroll');
		allPull = $(allScroll).pullToRefresh({
			up: {
				callback: function() {
					getAllData()
				}
			}
		});
		var perInfo = app.getState()
		nurseid = perInfo['loginPerID']
		setupBtnTapped('slider')
		getTodayData();
		getAllData()
	}
	function refreshMainPage(newIndex){
		if (newIndex == undefined) {
			resetTodayRefresh();
			resetAllRefresh()
			return
		}
		if (newIndex == 0){
			resetTodayRefresh();
		}else{
			resetAllRefresh()
		}
	}
	function resetTodayRefresh(){
		todayStart = 0;
		todayData = []
		if (todayPull) {
			todayPull.refresh(true)
		}
		getTodayData();
		$('#todayWrapper').scroll().scrollTo(0,0,100);
	}
	function resetAllRefresh(){
		allStart = 0;
		allData = []
		if (allPull) {
			allPull.refresh(true)
		}
		getAllData();
		$('#allWrapper').scroll().scrollTo(0,0,100);
	}
	var refreshPageData = refreshMainPage
	var exportObj = {}
	exportObj.refreshPageData = refreshPageData
	return exportObj
})(mui);
function refreshPageData(){
	errorObj.refreshPageData()
}
function toExam(){
	var indexStr = this.getAttribute('data-index')
	var sliderIndex = this.getAttribute('data-page')
	var firstChapter //一级数据
	if (sliderIndex == '0') {
		firstChapter = todayData[indexStr]
	}else{
		firstChapter = allData[indexStr]
	}
	
	var data = {rw:this.getAttribute('data-rw'),'nurseid':nurseid,'firstChapter':firstChapter}
	var webview = plus.webview.create('examChapterPaper.html', 'examChapterPaper', {
		background: '#fff',
		statusbar: {
			background: '#fff'
		}
	},{sourceData:data,fromPage:'examError'});
	webview.show('fade-in');
}